#!/bin/sh

## hyphop ##

#= make kernel wrapper

#/ USAGE $0 [ARGS]
#/ args:
#/ menuconfig
#/ dtbs
#/ -jN
#/ savedefconfig
#/ Image
#/ help
#/ ...

set -e 

D=$(dirname $0)
N=$(basename $0)
DR=$(realpath $D)

. $D/config.conf

[ -d $GCC_PATH ] || {
    echo "[e] GCC_PATH: $GCC_PATH - not found">&2
    exit 1
}

export PATH="$(realpath $GCC_PATH)/bin:$PATH"

export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
export CROSS_COMPILE=aarch64-none-linux-gnu-

DIR="$KERNEL_SOURCE_DIR"

[ -d "$DIR" ] || {
    echo "[e] source dir not found : $DIR">&2
    exit 1
}

CMD(){
    echo "[#] $@">&2
    "$@"
}

# cross gcc test
CMD ${CROSS_COMPILE}gcc --version  || {
    echo "[e] cross gcc failed">&2
    exit 1
}

DEF_CONFIG=kvims_defconfig

[ -f "$DIR/.config" ] || {
    echo "[i] config <= $DEF_CONFIG">&2
    T2=$DIR/arch/$ARCH/configs/$DEF_CONFIG
    truncate -s0 $T2
    #CMD cp $DR/../configs/linux-mainline/defconfig $T2
    for c in $DR/../configs/linux-mainline/defconfig_*; do
	[ -s $c ] || continue
	echo "[i] add defconfig ${c##*/}"
        cat "$c" >> $T2
    done
    CMD make -C "$DIR" $DEF_CONFIG
}

#ALL=
case "$1" in
    -n*)
    echo "[i] noop mode">&2
    exit 0
    ;;
    -a*)
    echo "[i] build all">&2
    ALL=1
    shift
    ;;
esac

ARGS="$@"

#[ "$ARGS" ] || {
case $ARGS in
    *-j*)
    ;;
    *)
    ARGS="$ARGS -j$(nproc)"
    ;;
esac
#}

[ "$DTB_ONLY" ] && ARGS="$ARGS dtbs"
[ "$DTB_AND_IMAGE" ] && ARGS="$ARGS dtbs Image.gz"
[ "$IMAGE_ONLY" ] && ARGS="$ARGS Image.gz"

CMD make -C "$DIR" $ARGS

case $ARGS in
    *modules*)
    echo "[i] build modules complited">&2
    exit
    ;;
esac

# post

MD5(){
    truncate -s0 "$1".md5sum
    md5sum "$1" > "$1".md5sum
}

[ "$NOPOST" ] && exit 0

T="$KERNEL_OUTPUT_DIR"

[ -d "$T" ] || mkdir -p "$T"

which rsync 2>/dev/null && rsync="rsync -av"
[ "$rsync" ] || rsync=cp

echo "[i] copy dtbs"

f="$T/fdt/amlogic"
[ -d "$f" ] || mkdir -p "$f"
CMD $rsync $DIR/arch/arm64/boot/dts/amlogic/*vim*.dtb "$f"
f="$T/fdt/rockchip"
[ -d "$f" ] || mkdir -p $f
CMD $rsync $DIR/arch/arm64/boot/dts/rockchip/*edge*.dtb "$f"

[ "$DTB_ONLY" ] && exit 0

CMD $rsync $DIR/kernel/config_data.gz $T/config.gz
#CMD gzip -c $DIR/.config > $T/config.gz

CMD $rsync $DIR/arch/arm64/boot/Image* "$T"
#CMD $rsync $DIR/arch/arm64/boot/Image "$T"
MD5 $T/Image

[ "$DTB_AND_IMAGE" ] && exit 0
[ "$IMAGE_ONLY" ] && exit 0

#KERNEL_ADDR=0x01080000
#KERNEL_ADDR=0x0f080000

gz=$(which pigz 2>/dev/null)
[ "$gz" ] || gz=gzip
mkimage=$(which mkimage 2>/dev/null)
lzma=$(which lzma 2>/dev/null)

(

$gz  -9c $T/Image > $T/Image.gz
# max gzip repack !!!
#advdef -z -4 $T/Image.gz

MD5 $T/Image.gz

CMD $mkimage -A arm64 \
    -O linux -C gzip -T kernel -a $KERNEL_ADDR -e $KERNEL_ADDR \
    -n kernel -d "$T/Image.gz" "$T/uImage.gzip"

MD5 "$T"/uImage.gzip

)&
(
$lzma -T4 -e -9 --block-size=3M -c $T/Image > $T/Image.lzma

MD5 "$T"/Image.lzma

CMD $mkimage -A arm64 \
    -O linux -C lzma -T kernel -a $KERNEL_ADDR -e $KERNEL_ADDR \
    -n kernel -d "$T/Image.lzma" "$T/uImage.lzma"

MD5 "$T"/uImage.lzma

)&

echo "[i] wait compression"

wait

[ "$ALL" ] && {
    ${0}_modules
}

cd $T

#DTBS=vims.dtb.tar.gz
#[ -f "$DTBS" ] && rm $DTBS
#tar -czf $DTBS *.dtb

DTBS=fdt.tar.gz
[ -f "$DTBS" ] && rm $DTBS

tar -czf $DTBS fdt && MD5 $DTBS

## OK BYBY
